/**
 * 公共跳转操作处理
 */
/* global $commonData:false */ 

export default class JumpByAction {
  constructor(app) {
    this.$app=app//app实例传入
    this.articlePageCount=0
    this.isJump=false // 是否可以跳
    this.startTime=0//开始时间
  }
  /**
   * 监测路由页面是否存在
   * @param {String} path 页面地址
   */
  checkExist(path){
    let stacks = this.$app.$def.router.getPages()
    stacks.map(item=>{
      if(item.path==path){
        this.articlePageCount++
        if(this.articlePageCount>=2){
          return true
        }
      }
      return false
    })
  }
  /** 
   * 初始化
   * @param {Object} data 跳转的参数  type 操作类型
   * @param {Function} callback  回调函数
  */
  jump(data,callback){
    let current=new Date().getTime()
    if(data&&current-this.startTime>500){
      this.startTime=new Date().getTime()
      if(data&&data.guide_type){
        this.$app.setAppData('newGuideIndex',data.guide_type)
        this.$app.$def.newGuideIndex=data.guide_type
        this.$app.$def.pubsubModel.publish('updateGuideIndex', this.$app.$def.newGuideIndex)
      }
      switch(data.type){
      case this.$app.$def.actionConfig.JUMP_APP_PAGE||this.$app.$def.actionConfig.JUMP_H5 :
        if(data.isReplace){
          this.$app.$def.router.replace({
            uri:data.url
          })
        }else{
          this.$app.$def.router.push({
            uri:data.url
          })
        }
        break
      case this.$app.$def.actionConfig.JUMP_APP_TAB:
        //  返回main 页面  待修复
        this.$app.$def.pubsubModel.publish(this.$app.$def.publishEvent.CHANGE_TAB,data.tabName)
        if(this.$app.$def.router.getState().name!='Main'){
          this.$app.setAppData('lastPageData',{
            pageName:data.from,
            tabName:''
          })
          console.log('=====切换tab',data.from)
          this.$app.$def.router.back({
            path:'/Main'
          })
        }
        if(data.closeDetail){
          this.$app.$def.pubsubModel.publish('controlDetail',{
            isShow:false
          })
        }
        break
      case this.$app.$def.actionConfig.JUMP_CLOSE :
        this.$app.$def.pubsubModel.publish(this.$app.$def.publishEvent.JUMP_CLOSE,null)
        break
      case this.$app.$def.actionConfig.JUMP_SHARE :
        this.$app.$def.youth.share(data.Share,()=>{
          if(callback){
            callback({
              type:data.type
            })
          }
        })
        break
      case this.$app.$def.actionConfig.SAVE_IMAGE :
        this.$app.$def.youth.saveImage(data.url,data.isHash,data.isShowToast)
        break
      case this.$app.$def.actionConfig.JUMP_ARTICLE:
        {
        // ___PARAM_LAUNCH_FLAG___:this.checkExist(path)?"clearTask":'',
          this.$app.$def.isDestroy = true // 标识跳转文章详情页
          data.extra_data?data.extra_data:data.extra_data = {exp_id:'',log_id:'',retrieve_id:'',strategy_id:''}
          data.current_module_sort?data.current_module_sort:data.current_module_sort = 0
          data.scene_id?data.scene_id:data.scene_id=''
          let path='/Page/ArticleDetails'
          let sendData={
            ...data.extra_data,
            current_module_sort:data.current_module_sort || '',
            scene_id:data.scene_id,// 来源 文章详情相关推荐
          }
          if(data.signature){
            this.$app.$def.pubsubModel.publish('controlDetail',{
              isShow:true,
              signature:data.signature,
              detailId:data.id,
              url:data.url,
              isFeed:data.isFeed?true:false,
              title:data.title,
              from:data.from||'',
              ...sendData
            })

          }else{
            this.$app.$def.router.push({
              uri:path,
              params:{
                detailId:data.id,
                jumpUrl:data.url,
                title:data.title,
                from:data.from||'',
                ...sendData
              }
            })
          }
          // 从视频详情跳转其他页面 上报视频结束事件 endPlayVideo
          if(data.content_duration && data.content_play_duration){
            let exData={content_duration:Math.floor(data.content_duration*1000),content_play_duration:Math.floor(data.content_play_duration*1000)}
            exData.$device_id=$commonData.device_id
            this.$app.$def.point.submitPoint('endPlayVideo',Object.assign(this.$app.$def.videoDetailsData,this.$app.$def.videoPageQuery),exData)
          }
        }
        
        break
      case this.$app.$def.actionConfig.JUMP_VIDEO:
        this.$app.$def.isDestroy = true // 标识视频详情页
        data.extra_data?data.extra_data:data.extra_data = {exp_id:'',log_id:'',retrieve_id:'',strategy_id:''}
        data.current_module_sort?data.current_module_sort:data.current_module_sort = 0
        data.scene_id?data.scene_id:data.scene_id=''
        this.$app.$def.router.push({
          uri:'/Page/VideoDetails',
          params: {
            detailId: data.id,
            jumpUrl:data.url,
            videoTitle:data.title,
            // 新加的参数
            ...data.extra_data,
            current_module_sort:data.current_module_sort,
            scene_id:data.scene_id,// 来源 视频详情相关推荐
          }
        })
        // 从视频详情跳转其他页面 上报视频结束事件 endPlayVideo
        if(data.content_duration && data.content_play_duration){
          let exData={content_duration:Math.floor(data.content_duration*1000),content_play_duration:Math.floor(data.content_play_duration*1000)}
          exData.$device_id=$commonData.device_id
          this.$app.$def.point.submitPoint('endPlayVideo',Object.assign(this.$app.$def.videoDetailsData,this.$app.$def.videoPageQuery),exData)
        }
        break
      case this.$app.$def.actionConfig.COPY_TEXT:
        this.$app.$def.clipboard.set({
          text: data.text,
          success:() => {
            callback&&callback()
          }
        })
        break 
      case this.$app.$def.actionConfig.INSTALL_APP :
        this.$app.$def.youth.installApp(data.isMyApp?'cn.youth.news':data.package,data.url,res=>{
          res.callbackAction=this.$app.$def.actionConfig.INSTALL_APP
          callback&&callback(res)
        })
        break
      case this.$app.$def.actionConfig.JUMP_ADD_DESK_ICON :
        this.$app.installDeskIcon(null,()=>{
          if(this.$app.$def.addDesktopReward){
            this.$app.$def.pubsubModel.publish('firstFailCb')
          }
        })
        break 
      case this.$app.$def.actionConfig.QUICK_WEIXIN_LOGIN :
        this.$app.quickWeixinLogin()
        break 
      case this.$app.$def.actionConfig.QUICK_WEIXIN_BIND :
        this.$app.quickBind()
        break  
      case this.$app.$def.actionConfig.HAS_INSTALLED_APP :
        this.$app.$def.youth.hasInstalledApp(data.package,res=>{
          callback&&callback({
            callbackAction:this.$app.$def.actionConfig.HAS_INSTALLED_APP,
            result:res
          })
        })
        break   
      }
    } 
  }
}
